<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表:优秀实践，copy domain修改列表
    private String name;
    private String ;
    private String registerTime;
    private String state;
    private String logo;
    private String address;
    private String admin_id;
		-->
		<el-table :data="pageData.data" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="店铺名" width="100" sortable>
			</el-table-column>
			<el-table-column prop="tel" label="联系方式" width="150" sortable>
			</el-table-column>
			<el-table-column prop="registerTime" label="注册时间" width="120" sortable>
			</el-table-column>
			<el-table-column prop="logo" label="图标" width="120" sortable>
                <!--<img src="http://47.95.117.210:8888/group1/M00/00/A8/rBE3kWD3_riATF8eAAC7qHhllI4928.jpg" class="image">-->
			</el-table-column>
			<el-table-column prop="address" label="地址" width="120" sortable>
			</el-table-column>
			<el-table-column prop="admin_id" label="员工" width="120" sortable>
			</el-table-column>

			<!--<el-table-column prop="state" label="状态" width="120" sortable :formatter="stateFormatter">-->
			<el-table-column prop="state" label="状态" width="100" sortable>
				<template scope="scope">
					<span style="color: blue" v-if="scope.row.state==0">待审核</span>
					<span style="color: green" v-if="scope.row.state==1">审核通过</span>
					<span style="color: red" v-if="scope.row.state==-1">驳回</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" min-width="120">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleAudit(scope.row)">审核</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="query.currentPage" :page-size="query.pageSize" :total="pageData.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面
		    v-model="editFormVisible" 老版本打开对话框
		    ref="editForm"
                    age:null,
                    state:0,
                    department_id:{},

		-->
		<el-dialog title="审核店铺" :visible.sync="shopAuditVisible" :close-on-click-modal="false">
			<el-form :model="shopAuditLog" label-width="80px"  ref="shopAuditLogForm">
				<el-form-item label="备注" prop="note">
					<el-input type="textarea" v-model="shopAuditLog.note"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="emlpoyeeVisible = false">取消</el-button>
				<el-button type="primary" @click.native="auditPass">通过</el-button>
				<el-button type="primary" @click.native="auditReject">驳回</el-button>
			</div>
		</el-dialog>

		<el-dialog :title="title"  :visible.sync="emlpoyeeVisible" :close-on-click-modal="false">
			<el-form :model="emlpoyee" label-width="80px" :rules="emlpoyeeFormRules" ref="emlpoyeeForm">
				<el-form-item label="用户名" prop="username">
					<el-input v-model="emlpoyee.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="emlpoyee.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="phone">
					<el-input v-model="emlpoyee.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="emlpoyee.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="年龄" prop="age">
					<el-input v-model="emlpoyee.age" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="部门">
					<el-select v-model="emlpoyee.department_id" value-key="name" clearable placeholder="请选择">
						<el-option
								v-for="item in department"
								:key="item.id"
								:label="item.name"
								:value="item">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="emlpoyeeVisible = false">取消</el-button>
				<el-button type="primary" @click.native="emlpoyeeSubmit" :loading="emlpoyeeLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
    export default {
        data() {
            return {
                pageData:{
                    //分页条
                    total:0,
                    //数据表格
                    data:[]
                },
                query:{
                    // 搜索关键字
                    keyword:'',
                    //分页条
                    currentPage:1,
                    //分页条
                    pageSize:10
                },
				shops:{},
				department:{},
                deptTree:[],
				title:"",
				msg:'',
                listLoading: false,
                sels: [],//列表选中列

                emlpoyeeVisible: false,//编辑界面是否显示
                shopAuditVisible:false,
                emlpoyeeLoading: false,
                emlpoyeeFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                emlpoyee: {
                    id:null,
                    username:"",
                    email:"",
                    phone:"",
                    password:"",
                    age:null,
                    state:0,
                    department_id:null
                },
                shopAuditLog:{
                    shop_id:null,
                    note:''
				}
            }
        },
        methods: {
            //状态显示转换-不能上色
            // stateFormatter: function (row, column) {
            //    if(row.state==0){
            //        return "正常"
            // 	}else{
            //        return "<font style='color: red;'>禁用</font>"
            // 	}
            // },
            search(){
                //修改页码
                this.query.currentPage =1;
                //改查询条件=双向绑定输入时自动改变
                this.getShop();
            },
            handleCurrentChange(val) {
                this.query.currentPage = val;
                this.getShop();
            },
            //获取用户列表
            getShop(){
            	//加载条
                this.listLoading = true;
                this.$http.post("/shop",this.query)
                    .then(result => {
                        // debugger;
                        this.pageData = result.data;
                        console.debug(this.pageData);
                        //去除加载条
                        this.listLoading = false;
                    })
                    .catch(result=>{
                        alert("错了！");
                    })

            },
            //删除
            handleDel: function (index, row) {

                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(()=>{
                    // 开启加载图标
					this.listLoading = true;
					this.$http.delete("/shop/"+row.id).then(result=>{
					    if (result.data.success){
                            this.$message({
                                message: "删除成功",
                                type: 'success'
                            });
						}else {
                            this.$message({
                                message:"删除失败",
                                type:'error'
                            })
						}
                        this.listLoading = false;
                        this.getShop();
					}).catch(result=>{
					    console.log(result)
					    alert("报错")
					})
				})
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.title = "编辑界面"
                this.emlpoyeeVisible = true;
                this.getEmployee();
                this.getDept();
                this.emlpoyee = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.title = "新增界面"
                this.emlpoyeeVisible = true;
                this.getEmployee();
                this.getDept();
                // 置空
                this.emlpoyee = {
                    id:null,
                    username:"",
                    email:"",
                    phone:"",
                    password:"",
                    age:null,
                    state:0,
                    department_id:null
                };
            },
            //编辑
            emlpoyeeSubmit: function () {
                this.$refs.emlpoyeeForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.emlpoyeeLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.emlpoyee);
                            let paraArr = para.department_id;
                            console.debug(paraArr);
							this.$http.put("/shop",para)
								.then(result=>{
                                    this.emlpoyeeLoading = false;
								    if (result.data.success){
								        this.$message({
											message:"提交成功",
											type:"success"
										})
                                        this.emlpoyeeVisible = false; //关闭对话框
                                        this.$refs['emlpoyeeForm'].resetFields();//重置表单
                                        this.getShop(); //重新加载
									}else {
                                        this.$message({
                                            message:"提交失败",
                                            type:"error"
                                        })
									}

								})
                        }).catch(result=>{
                            console.debug(result)
							alert("报错")
						})
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/shop",ids)
						.then(result=>{
						    if(result.data.success){
						        this.$message({
                                    message: "删除成功",
                                    type: 'success'
                                });
							}else {
                                this.$message({
                                    message: "删除失败",
                                    type: 'error'
                                });
							}
                            this.listLoading = false;
                            this.getShop();
							// 删除后跳到第一页
							this.handleCurrentChange(1);
						})
                }).catch(() => {
					alert("报错")
                });
            },
            getEmployee(){
                this.$http.get("/shop")
                    .then(result=>{
                        if(result){
                            console.debug(result.data)
                            this.shops = result.data
                        }
                    })
            },
            getDeptTree(){
                this.$http.get("/emlpoyee/tree")
                    .then(result=>{
                        console.debug(result.data);
                        this.deptTree = result.data;
                    })
            },
            getDept(){
                this.$http.get("/department")
                    .then(result=>{
                        this.department = result.data;
                    })
            },
            handleAudit(row){
                this.shopAuditVisible=true;
				this.shopAuditLog.shop_id =row.id;
			},
            auditPass(){
                this.$http.post("/shop/audit/pass",this.shopAuditLog)
					.then(result=>{
                        if(result.data.success){
                            this.$message({
                                message: "审核成功",
                                type: 'success'
                            });
                        }else {
                            this.$message({
                                message: "审核失败",
                                type: 'error'
                            });
                        }
                        this.shopAuditVisible=false;
                        this.getShop();
                        this.shopAuditLog={
                            shop_id:null,
                            note:''
                        }
					})
			},
            auditReject(){
                this.$http.post("/shop/audit/reject",this.shopAuditLog)
					.then(result=>{
                        if(result.data.success){
                            this.$message({
                                message: "驳回成功",
                                type: 'success'
                            });
                        }else {
                            this.$message({
                                message: "驳回失败",
                                type: 'error'
                            });
                        }
                        this.shopAuditVisible=false;
                        this.getShop();
                        this.shopAuditLog={
                            shop_id:null,
                            note:''
                        }
					})
			}
        },
        mounted() {
            this.getShop();
        }
    }

</script>

<style scoped>

</style>